package com.example.task1.http;

import android.app.Activity;
import android.app.AlertDialog;
import android.app.ProgressDialog;

import com.example.task1.application.MyApplication;
import com.example.task1.util.CallBack;
import com.example.task1.util.ApiResp;
import com.google.gson.Gson;

import org.jetbrains.annotations.NotNull;
import org.json.JSONObject;

import java.io.File;
import java.io.IOException;
import java.util.HashMap;
import java.util.concurrent.TimeUnit;

import okhttp3.MediaType;
import okhttp3.MultipartBody;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.RequestBody;
import okhttp3.ResponseBody;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
import retrofit2.Retrofit;
import retrofit2.converter.gson.GsonConverterFactory;
import retrofit2.http.Body;
import retrofit2.http.DELETE;
import retrofit2.http.GET;
import retrofit2.http.Header;
import retrofit2.http.POST;
import retrofit2.http.PUT;
import retrofit2.http.Part;
import retrofit2.http.Url;

public class Api {

    public static int pageSize = 20;

    public static Gson gson = new Gson();

    public static Api.ApiService api() {
        OkHttpClient client = new OkHttpClient().newBuilder().connectTimeout(1, TimeUnit.SECONDS)
                .build();
        return new Retrofit.Builder()
                .addConverterFactory(GsonConverterFactory.create(new Gson()))
                .client(client)
                .baseUrl(MyApplication.BASE_URL)
                .build().create(Api.ApiService.class);
    }

    public static void send(Call<ApiResp> call, Class bean, CallBack back) {
        call.enqueue(new Callback<ApiResp>() {
            @Override
            public void onResponse(Call<ApiResp> call, Response<ApiResp> response) {
                ApiResp body = response.body();
                okhttp3.Response raw = response.raw();
                if (body.getCode() == 200) {
                    back.success(gson.fromJson(gson.toJson(body), bean));
                } else {
                    MyApplication.show(body.getMsg());
                    back.error(body.getMsg());
                }
            }

            @Override
            public void onFailure(Call<ApiResp> call, Throwable throwable) {
                MyApplication.show(throwable.getMessage());
            }
        });
    }

    public static RequestBody getBody(HashMap<String, Object> map) {
        MediaType mediaType = MediaType.parse("application/json");
        return RequestBody.create(mediaType, new JSONObject(map).toString());
    }

    public static RequestBody getFileBody(File flie) {
        return new MultipartBody.Builder().setType(MultipartBody.FORM)
                .addFormDataPart("file", flie.getName(), RequestBody.create(MediaType.parse("multipart/form-data"), flie))
                .build();
    }


    public interface ApiService {


        @GET
        Call<ApiResp> get(@Url String url);

        @GET
        Call<ApiResp> get(@Header("Authorization") String token, @Url String url);

        @GET
        Call<ResponseBody> gets(@Header("Authorization") String token, @Url String url);

        @POST
        Call<ApiResp> post(@Url String url, @Body RequestBody body);

        @POST
        Call<ApiResp> post(@Header("Authorization") String token, @Url String url, @Body RequestBody body);

        @POST
        Call<ApiResp> post(@Header("Authorization") String token, @Url String url, @Part MultipartBody.Part file);

        @PUT
        Call<ApiResp> put(@Url String url, @Body RequestBody body);

        @PUT
        Call<ApiResp> put(@Header("Authorization") String token, @Url String url, @Body RequestBody body);

        @DELETE
        Call<ApiResp> del(@Url String url, @Body RequestBody body);

        @DELETE
        Call<ApiResp> del(@Header("Authorization") String token, @Url String url, @Body RequestBody body);

        @DELETE
        Call<ApiResp> del(@Header("Authorization") String token, @Url String url);

    }

}
